package bamin.com.bamindaijia.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.aiton.administrator.shane_library.shane.widget.SingleBtnDialog;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

import bamin.com.bamindaijia.R;
import bamin.com.bamindaijia.adapter.InvoiceHistoryAdapter;
import bamin.com.bamindaijia.constant.Constant;
import bamin.com.bamindaijia.model.AllOrder;
import bamin.com.bamindaijia.model.InvoiceHistory;
import cz.msebera.android.httpclient.Header;

public class InvoiceHistoryActivity extends ZjbBaseActivity implements View.OnClickListener {

    private ListView mList_invoice_history;
    private int mUserId;
    private AllOrder allOrder;
    private int orderPageCount = 1;
    private List<InvoiceHistory.DatasBean> mDatas = new ArrayList<>();
    private InvoiceHistoryAdapter mOrdersAdapter;
    private TextView mTv_invoice_null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invoice_history);
        showLoadingDialog();

        init();

    }

    @Override
    protected void initData() {
        loadOrder();
    }

    @Override
    protected void initViews() {
        mOrdersAdapter = new InvoiceHistoryAdapter(this, mDatas);
        mList_invoice_history.setAdapter(mOrdersAdapter);
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageView_back).setOnClickListener(this);

    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        mUserId = intent.getIntExtra(Constant.INTENT_KEY.USER_ID, 0);

    }

    @Override
    protected void findID() {
        mList_invoice_history = (ListView) findViewById(R.id.list_invoice_history);
        mTv_invoice_null = (TextView) findViewById(R.id.tv_invoice_null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView_back:
                finishTo();
                break;
        }
    }

    private void loadOrder() {
        String url = Constant.Url.LOAD_ORDER_RECEIPT;
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("customerId", "" + mUserId);
        asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String s = new String(responseBody);
                try {
                    Log.e("AllOrderActivity", "已开票订单: --->>" + s);

                    InvoiceHistory invoice = GsonUtils.parseJSON(s, InvoiceHistory.class);
                    if (invoice.getStatus() == 0) {
                        List<InvoiceHistory.DatasBean> datas = invoice.getDatas();
                        if (datas.size() > 0) {
                            mDatas.addAll(datas);
                        } else {
                            Toast.makeText(InvoiceHistoryActivity.this, "数据为空", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(InvoiceHistoryActivity.this, invoice.getMes(), Toast.LENGTH_SHORT).show();
                    }
                    mOrdersAdapter.notifyDataSetChanged();
                    cancelLoadingDialog();

                } catch (Exception e) {
                    cancelLoadingDialog();
                    Log.e("AllOrderActivity", "下载订单失败");

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.e("AllOrderActivity", "下载订单失败");
                cancelLoadingDialog();
                showSingleDialog();
            }
        });
    }

    private void showSingleDialog() {
        final SingleBtnDialog singleBtnDialog = new SingleBtnDialog(this, "网络请求失败", "确定");
        singleBtnDialog.setClicklistener(new SingleBtnDialog.ClickListenerInterface() {
            @Override
            public void doWhat() {
                finishTo();
                singleBtnDialog.dismiss();
            }
        });
        singleBtnDialog.show();
    }


}
